using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using QFramework;
using Unity.VisualScripting;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;

namespace ProjectSurvival
{
    public partial class EnemyGenerator : ViewController
    {
        [SerializeField] public LevelConfig lvConfig;

        //序列化为可配置波次的列表
        //[SerializeField] private List<EnemyWave> waveList = new List<EnemyWave>();

        //队列
        private Queue<EnemyWave> _enemyQueue = new Queue<EnemyWave>();

        //生成敌人时间
        private float _currentGenerateTime = 0;

        private Player _player;

        [HideInInspector]
        //当前的波次
        public EnemyWave CurrentWave = null;

        //当前波次的持续时间
        private float _currentWaveSeconds = 0;

        public static BindableProperty<int> EnemyCount = new BindableProperty<int>(0);

        //当前波次 
        public int WaveCount = 0;

        //波次总数
        private int totalCount = 0;

        //判断当前波次是否是最后一波
        public bool IsLastWave => WaveCount == totalCount;

        private void Start()
        {
            CurrentWave = null;
            _player = Player.Instance;

            //将配置列表放入队列
            foreach (var group in lvConfig.EnemyWaveGroups)
            {
                foreach (var enemyWave in group.EnemyWaves)
                {
                    _enemyQueue.Enqueue(enemyWave);
                    totalCount++;
                }
            }
        }

        private void Update()
        {
            //如果当前波次结束，且还有后续波次，则时候后续波次的配置
            if (CurrentWave == null)
            {
                if (_enemyQueue.Count > 0)
                {
                    WaveCount++;
                    CurrentWave = _enemyQueue.Dequeue();
                    _currentGenerateTime = 0;
                    _currentWaveSeconds = 0;
                }
            }

            //如果当前波次未结束，则按当前波次的配置生成敌人
            if (CurrentWave != null)
            {
                _currentWaveSeconds += Time.deltaTime;
                _currentGenerateTime += Time.deltaTime;
                if (_currentGenerateTime >= CurrentWave.generatorDuration)
                {
                    _currentGenerateTime = 0;

                    if (_player)
                    {
                        _currentGenerateTime = 0;
                        CurrentWave.enemyPrefab.Instantiate()
                            .Position(GetRandomPositionOnBorder())
                            .Self((self) =>
                            {
                                var enemy = self.GetComponent<IEnemy>();
                                enemy.SetSpeedScale(CurrentWave.speedScale);
                                enemy.SetHpScale(CurrentWave.hpScale);
                            })
                            .Show();
                    }
                }

                if (_currentWaveSeconds >= CurrentWave.waveSeconds)
                {
                    CurrentWave = null;
                }
            }
        }

        private Vector2 GetRandomPositionOnBorder()
        {
            var choseXY = RandomUtility.Choose(-1, 1);
            Vector2 newPosition = Vector2.zero;

            if (choseXY == -1)
            {
                newPosition.x = RandomUtility.Choose(CameraController.instance.LB.position.x,
                    CameraController.instance.RT.position.x);
                newPosition.y = Random.Range(CameraController.instance.LB.position.y,
                    CameraController.instance.RT.position.y);
            }
            else
            {
                newPosition.x = Random.Range(CameraController.instance.LB.position.x,
                    CameraController.instance.RT.position.x);
                newPosition.y = RandomUtility.Choose(CameraController.instance.LB.position.y,
                    CameraController.instance.RT.position.y);
            }

            return newPosition;
        }
    }
}